﻿using System;
using WikiPlex.Common;
using WikiPlex.Formatting;

namespace WikiPlex.Contrib.Renderers
{
    /// <summary>
    /// Renderer for [color:ff0000|Text that will appear red] sections
    /// </summary>
    public class ColoredTextRenderer : IRenderer
    {
        public string Id
        {
            get { return "Colored Text"; }
        }

        public bool CanExpand(string scopeName)
        {
            return scopeName.Equals(ContribScopeNames.ColoredText);
        }

        public string Expand(string scopeName, string input, Func<string, string> htmlEncode, Func<string, string> attributeEncode)
        {
            try
            {
                TextPart parts = Utility.ExtractTextParts(input);
                return string.Format("<span style=\"color:#{0};\">{1}</span>", parts.FriendlyText, htmlEncode(parts.Text));
            }
            catch (ArgumentException)
            {
                return RenderUnresolvedMacro();
            }
        }

        private static string RenderUnresolvedMacro()
        {
            return "<span class=\"unresolved\">Cannot resolve colored text macro, invalid number of parameters.</span>";
        }

    }
}
